/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import java.io.File;
import javax.swing.JTable;

/**
 *
 * @author Huynh Duy Khuong
 */
public class FileUtilities {

    /** Lay ten file khong co phan mo rong
     *
     * @param fnExt: ten file
     * @return Ten file khong co phan mo rong
     */
    public static String getNameWithoutExt(String fnExt) {
        int dotPos = fnExt.lastIndexOf(".");

        if (dotPos != -1) {
            return fnExt.substring(0, dotPos);
        }
        return fnExt;
    }

    /** Lay ten file khong co phan mo rong
     *
     * @param f File can lay ten khong co phan mo rong
     * @return Ten file khong co phan mo rong
     */
    public static String getNameWithoutExt(File f) {
        String fnExt = f.getName();
        if (f.isDirectory()) {
            return fnExt;
        }
        return getNameWithoutExt(fnExt);
    }

    /** Lay phan mo rong cua file
     *
     * @param f File can lay
     * @return Phan mo rong cua file. Hoac tra ve "" neu file khong co phan mo rong
     */
    public static String getExtension(File f) {
        String fnExt = f.getName();
        if (f.isDirectory()) {
            return "";
        }
        return getExtension(fnExt);
    }

    /** Lay phan mo rong cua mot ten file
     *
     * @param fnExt: Chuoi can lay
     * @return Phan mo rong cua ten file. Hoac "" neu ten file khong co phan mo rong
     */
    public static String getExtension(String fnExt) {
        int dotPos = fnExt.lastIndexOf(".");

        if (dotPos != -1) {
            return fnExt.substring(dotPos + 1);
        }
        return "";
    }

    /** Kiem tra xem ten file co phan mo rong hay khong
     *
     * @param filename Ten file can kiem tra
     * @return true neu ten file co phan mo rong. Nguoc lai tra ve false
     */
    public static boolean haveExtension(String filename) {
        return (filename.indexOf(".") == -1) ? false : true;
    }

    /** Kiem tra xem file co phan mo rong hay khong
     *
     * @param f file can kiem tra
     * @return true neu file co phan mo rong. Nguoc lai tra ve false
     */
    public static boolean haveExtension(File f) {
        return (f.getName().indexOf(".") == -1) ? false : true;
    }

    /** Gan them phan mo rong vao ten file
     *
     * @param fnWithoutExt ten file can gan
     * @param ext phan mo rong
     * @return Ten file da gan them phan mo rong
     */
    public static String appendExt(String fnWithoutExt, String ext) {
        String fnExt = fnWithoutExt + "." + ext;
        return fnExt;
    }

    /** Lay file tai dong dang duoc chon trong JTable
     *
     * @param table Bang muon lay
     * @return File tai dong dang duoc chon trong JTable
     */
    public static File getSelectedFileInTable(JTable table) {
        File f;
        int rowIndexView = table.getSelectedRow();
        int rowIndexModel = table.convertRowIndexToModel(rowIndexView);
        int columnIndex = 0;
        MyLabel label = (MyLabel) table.getModel().getValueAt(rowIndexModel, columnIndex);
        f = (File) label.getTag();

        return f;
    }

    /** Lay files tai nhung dong duoc chon trong bang
     *
     * @param table Bang muon lay
     * @return Nhung file tai nhung dong duoc chon
     */
    public static File[] getSelectedFilesInTable(JTable table) {
        File f[];

        int[] rowIndexView = table.getSelectedRows();
        int[] rowIndexModel = new int[rowIndexView.length];
        for (int i = 0; i < rowIndexView.length; i++) {
            rowIndexModel[i] = table.convertRowIndexToModel(rowIndexView[i]);
        }

        int columnIndex = 0;
        MyLabel label;

        f = new File[rowIndexModel.length];
        for (int j = 0; j < rowIndexModel.length; j++) {
            label = (MyLabel) table.getModel().getValueAt(rowIndexModel[0], columnIndex);
            f[j] = (File) label.getTag();
        }

        return f;
    }

}